<?php
class Display_invoice_model extends CI_MODEL
	{
		public function __construct()
			{
				parent::__construct();
				$this->load->database();
				
			}
		function invoice_export_client_address($project_id)
			{
				$sql = "SELECT client_id FROM `projects` WHERE `id` = ".$this->db->escape($project_id);
				$client_id = $this->db->query($sql)->row_array();
				$tmp = $client_id['client_id'];
				//echo($sql);
				//print_r($client_id['client_id']);
				//exit;
				//echo("<br/>" . $tmp . ":Client _id<br/>");
				$sql = "SELECT address,city,state,country FROM `client_details` WHERE `client_id` = ".$this->db->escape($tmp);
				$client_address = $this->db->query($sql)->row_array();
					
				//print_r($client_address);
				//echo("<br/>".$client_address['country']);
				//exit;
				//$this->db->select('address,city,state,country'); 
				//$client_address = $this->db->get_where('client_details',array('id' = $client_id));
				
				if (!isset($client_address))
				$client_address['address']=" ";
				$client_address['city']=" ";
				$client_address['state']=" ";
				$client_address['country']=" ";
						
				$address = $client_address['address'] . "," . $client_address['city'] . ",&#10;" . $client_address['state'] . "," . $client_address['country'];
				return $address;
			}
			
		function invoice_export_task_static_details($box)
			{
				for($i=0;$i<count($box);$i++)
					{
						$tmp = $box[$i];
						//echo($tmp);
						//$sql = "SELECT 'title' ,'desc' ,'time_spent FROM' 'tasks' WHERE 'id' = " . $this->db->escape($tmp);
						//$query = $this->db->query($sql)->row_array();
						$this->db->select('title,desc');
						$query = $this->db->get_where('tasks',array('id' => $tmp));
						$sql = $this->db->last_query();
						//echo($sql);
						//echo("<br/>");
						//print_r($query->row_array());
						//echo("<br/><br/>");
						$ar[$i] = $query->row_array();
						//$query[$i] = $query->row_array();
						//$str = $this->db->last_query();
						//echo("<br/>");
						//echo($str);
						//echo("<br/>");
						//print_r($sql);
						
						//print_r($query);
						//exit;

						
					}
				return $ar;	
				//return $query;	
				//echo("<br/><br/>ro array:");
				//print_r($ar);
				//exit;
			}
		
		function convert_timeto_seconds()
			{
				
			}
		
		function invoice_export_time_dynamic($project_id,$box,$type=NULL)
			{
				$this->db->select('id');
				$query = $this->db->get_where('tasks',array('project_id' => $project_id));
				$task_id = $query->result_array();
												
				for($i = 0;$i<(count($box));$i++)
					{
						$this->db->select('time');
						$query = $this->db->get_where('invoice_info',array('task_id' => $box[$i]));
						$time_spent_per_task = $query->result_array();
						
						
						$k=0;
						$time = array();					
						foreach ($time_spent_per_task as $row)
							{ 
								$time[$k] = $row['time'];
								$k++;
							}	
						
						
						$tmp_ttl=0;
						for($j=0;$j<count($time);$j++)
							{
								$tmp_time1 = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $time[$j]);
								sscanf($tmp_time1, "%d:%d:%d", $hours, $minutes, $seconds);
								$tmp_time1 = $hours * 3600 + $minutes * 60 + $seconds;
								$tmp_ttl = $tmp_ttl + $tmp_time1;
							}
						$this->db->select('time_spent');
						$query = $this->db->get_where('tasks',array('project_id' => $project_id,'id' => $box[$i]));
						$tmp = $query->row_array();
						$tmp = $tmp['time_spent'];
						$tmp_time2 = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $tmp);
						sscanf($tmp_time2, "%d:%d:%d", $hours, $minutes, $seconds);
						$tmp_time2 = $hours * 3600 + $minutes * 60 + $seconds;
						$time_remaining = $tmp_time2-$tmp_ttl; 
						$seconds = $time_remaining;
						$hours = floor($seconds/3600);
						$remainder_1 = ($seconds % 3600);
						$minutes = floor($remainder_1 / 60);
						$seconds = ($remainder_1 % 60);
							if(strlen($hours) == 1) 
								{
									$hours = "0".$hours;
								}
							if(strlen($minutes) == 1) 
								{
									$minutes = "0".$minutes;
								}
							if(strlen($seconds) == 1) 
								{
									$seconds = "0".$seconds;
								}
							$display_time[$i] = $hours.":".$minutes.":".$seconds;
					}
				return $display_time;
		}
		
		function invoice_export_task_custom_rates($project_id,$box)
			{
				//echo("</br>");
				//print_r ($box);
				//echo("</br>");
				
				for($i=0;$i<count($box);$i++)
					{
						$this->db->select('rate');
						$query = $this->db->get_where('invoice_custom_data',array('project_id' => $project_id,'task_id' => $box[$i]));
						//$query = array_filter($query);
						//if (!empty($query)) 
							//{
							//	$invoice_rates[$i] = $query->row_array();
							//}
						//else
							//{
								$invoice_rates[$i] = 0;
							//}
					}	
					//echo("<br/>");
					//echo("<br/>");
					//echo($invoice_rates);
					//print_r($invoice_rates);
					//exit;
				return $invoice_rates;	
				//print_r($invoice_rates);
				//return $invoice_rates->result_array();
			}	
			
			
		function invoice_export_tax_custom()
			{
				$query = $this->db->query("SELECT tax from `tax_recent`");
				return $query->row_array();
			}
		
		function invoice_number()
			{
				$query = $this->db->query('SELECT invoice_id FROM invoice_info ORDER BY invoice_id DESC LIMIT 1;');
				
				
				//print_r($query);
				$query = $query->row_array();
				//print_r($query);
				
				
				
				//echo ($query['invoice_id']);
				$query = array_filter($query);
				
				if (!empty($query)) 
					{
						//echo("Not empty");
						$tmp = $query['invoice_id'];
						//print_r($query);
						//echo($tmp);
						return $tmp+1;
					}
				else 
					{
						//echo($query['invoice_id']);
						//echo("Empty");
						return 1;
					}
			}
	}

?>